package com.example.myfirstapplication

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.example.myfirstapplication.adapter.NewListAdapter
import com.example.myfirstapplication.data.News
import com.example.myfirstapplication.utils.BaseActivity
import com.example.myfirstapplication.utils.Tag
import com.example.myfirstapplication.utils.myToast
import kotlinx.android.synthetic.main.activity_listview_layout.*
import kotlinx.android.synthetic.main.activity_main.topBar

class ListViewActivity : BaseActivity() {
    private var newsList: Array<String>? = null
    private var newsList2: ArrayList<News> = ArrayList()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_listview_layout)
        topBar.setLeftBtnText("<返回")
        topBar.setLeftBtnOnClickCallback {
            val intentObj = Intent(this, MainActivity::class.java)
            startActivity(intentObj)
            finish()
        }
        topBar.hideLeftBar()
        topBar.setTitle("ListView使用")
        initNews()
        Log.i(Tag, newsList2?.toString())
    //    val listAdapter=ArrayAdapter(this,android.R.layout.simple_list_item_1,newsList!!)
        listView.adapter=NewListAdapter(this,newsList2)
        listView.setOnItemClickListener { parent, view, position, id ->
            myToast(
                this,
                "你点击的新闻是" + newsList2!![position]
            )
        }
    }

    private fun initNews() {
        for (i in 1..30) {
            val news = News(
                "$i",
                "第${i}条新闻",
                "http://www.jci.edu.cn/images/P420200501.jpg"
            )
            newsList2.add(news)
        }
    }
}
